<template>
  <a-modal
    title="操作"
    style="top: 20px;"
    :width="800"
    v-model="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit">
    <a-form :form="form">
      <a-form-item style="display:none"><a-input v-decorator="['id']" /></a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="标题">
        <a-input placeholder="标题" v-decorator="['title', { rules: [{ required: true, message: '请填写新闻标题' }] }]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="作者"><a-input placeholder="作者" v-decorator="['author']" /></a-form-item>

      <a-row>
        <a-col :span="12">
          <a-form-item :labelCol="rlabelCol" :wrapperCol="rwrapperCol" label="类型">
            <a-select placeholder="请选择类型" v-decorator="['type', { rules: [{ required: true, message: '请选择类型' }] }]" allowClear>
              <a-select-option v-for="(b, index) in typeList" :key="index" :value="b.dictValue">{{ b.dictLabel }}</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item :labelCol="rlabelCol" :wrapperCol="rwrapperCol" label="状态">
            <a-select placeholder="请选择状态" v-decorator="['status']">
              <a-select-option v-for="(b, index) in statusList" :key="index" :value="b.dictValue">{{ b.dictLabel }}</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>

      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="内容">
        <a-input style="display: none;" v-decorator="['content']" />
        <tinymce v-model="content" :key="tinymceFlag"></tinymce>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { saveSysNews } from '@/api/news'
import Tinymce from '@/components/Editor/Tinymce'
import pick from 'lodash.pick'
export default {
  name: 'NewsModal',
  props: {
    typeList: {
      type: Array,
      required: true
    },
    statusList: {
      type: Array,
      required: true
    }
  },
  components: { Tinymce },
  data () {
    return {
      content: '',
      tinymceFlag: 1,
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      rlabelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      rwrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      mdl: {},
      form: this.$form.createForm(this)
    }
  },
  beforeCreate () {},
  created () {},
  activated () {
    this.tinymceFlag++
  },
  watch: {
    content (value) {
      this.form.setFieldsValue({
        content: value
      })
    }
  },
  methods: {
    add () {
      this.form.resetFields()
      this.content = ''
      this.edit({ id: 0 })
    },
    edit (record) {
      this.mdl = Object.assign(record)
      this.visible = true
      if (record.content) {
        this.content = record.content
      } else {
        this.content = ''
      }
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.mdl, 'id', 'title', 'author', 'type', 'content', 'status'))
      })
    },
    handleSubmit (e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          console.log('Received values of form: ', values)
          this.confirmLoading = true
          saveSysNews(values)
            .then(res => {
              if (res.code === 0) {
                this.$message.success('保存成功')
                this.$emit('ok')
                this.visible = false
              } else {
                this.$message.error(res.msg)
              }
            })
            .catch(() => {
              this.$message.error('系统错误，请稍后再试')
            })
            .finally(() => {
              this.confirmLoading = false
            })
        }
      })
    }
  }
}
</script>
